﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Management;
using System.Diagnostics;
using System.Threading;


namespace MusicForMyBlog
{
    public partial class Form1 : Form
    {
        App_Code.TrayApp ta = new App_Code.TrayApp();
        
        

        public Form1()
        {
            InitializeComponent();
            ta.Start();
            CheckProcesses();

            
        }

        private void CheckProcesses()
        {
            if (Process.GetProcessesByName("iTunes").Length > 0)
            {
                ta.Status = "iTunes is running..";
                ta.CreateStatusSubMenu();
                App_Code.Itunes itunes = new App_Code.Itunes();
                itunes.Start();
                ManagementEventWatcher watcher = WatchForProcessEnd("iTunes.exe");
                watcher.Dispose();
            }
            else
            {
                ta.Status = "iTunes not running..";
                ManagementEventWatcher watcher = WatchForProcessStart("iTunes.exe");
                watcher.Dispose();
            }



            //string processes = "";
            //foreach (Process p in Process.GetProcesses())
            //    processes = processes + ", " + p.ProcessName;

            //MessageBox.Show(processes);
            
        }

        private ManagementEventWatcher WatchForProcessStart(string processName)    
        {        
            string queryString =             
                "SELECT TargetInstance" +             
                "  FROM __InstanceCreationEvent " +             
                "WITHIN  5 " +            
                " WHERE TargetInstance ISA 'Win32_Process' " +            
                "   AND TargetInstance.Name = '" + processName + "'";   
       
            // The dot in the scope means use the current machine         
            string scope = @"\\.\root\CIMV2";  
        
            // Create a watcher and listen for events        
            ManagementEventWatcher watcher = new ManagementEventWatcher(scope, queryString);         
            watcher.EventArrived += ProcessStarted;         
            watcher.Start();         
            return watcher;     
        }

        private void ProcessStarted(object sender, EventArrivedEventArgs e) 
        {

            ta.Status = "iTunes is running..";
            ta.CreateStatusSubMenu();
            App_Code.Itunes itunes = new App_Code.Itunes();
            itunes.Start();
            ManagementEventWatcher watcher = WatchForProcessEnd("iTunes.exe");
            watcher.Dispose();
            
        }

        private ManagementEventWatcher WatchForProcessEnd(string processName)    
        {         
            string queryString =             
                "SELECT TargetInstance" +             
                "  FROM __InstanceDeletionEvent " +             
                "WITHIN  5 " +             
                " WHERE TargetInstance ISA 'Win32_Process' " +             
                "   AND TargetInstance.Name = '" + processName + "'";         
            // The dot in the scope means use the current machine        
            string scope = @"\\.\root\CIMV2";          
            // Create a watcher and listen for events        
            ManagementEventWatcher watcher = new ManagementEventWatcher(scope, queryString);         
            watcher.EventArrived += ProcessEnded;        
            watcher.Start();         
            return watcher;     
        }

        private void ProcessEnded(object sender, EventArrivedEventArgs e) 
        { 

            ta.Status = "iTunes not running..";
            ta.RemoveStatusSubMenu();
            ManagementEventWatcher watcher = WatchForProcessStart("iTunes.exe");
            watcher.Dispose();
        }  

        protected override void OnLoad(EventArgs e)
        {
            Visible = false;
            ShowInTaskbar = false;

            base.OnLoad(e);
        }

        

    }
}
